package com.dede.screensaverhelper

import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.IBinder
import com.dede.screensaverhelper.activity.DreamTempActivity


/**
 * @author hsh
 * @since 2018/2/8 10:27.
 */
class DreamServer : Service() {

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        val filter = IntentFilter()
        filter.addAction(Intent.ACTION_SCREEN_OFF)
        filter.addAction(Intent.ACTION_SCREEN_ON)
        registerReceiver(screenStateReceiver, filter)
    }

    private val screenStateReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent == null) return
            val action = intent.action
            when (action) {
                Intent.ACTION_SCREEN_ON -> {
                }
                Intent.ACTION_SCREEN_OFF -> {
                    val activity = Intent(context, DreamTempActivity::class.java)
                    activity.flags = Intent.FLAG_ACTIVITY_NEW_TASK
//                    activity.putExtra("delay", true)
                    context!!.startActivity(activity)
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(screenStateReceiver)
    }
}